package com.happy3w.ssp.schoolmatehome.service.template;

import com.happy3w.ssp.schoolmatehome.entity.ClockInTemplate;
import com.happy3w.ssp.schoolmatehome.entity.User;
import com.happy3w.ssp.schoolmatehome.exception.MessageException;
import com.happy3w.ssp.schoolmatehome.repository.ClockInTemplateRepository;
import com.happy3w.ssp.schoolmatehome.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class ClockInTemplateService {
    private final ClockInTemplateRepository clockInTemplateRepository;
    private final UserRepository userRepository;

    public ClockInTemplate create(ClockInTemplate template, String wxUnionId) {
        Optional<User> userOpt = userRepository.findByWxUnionId(wxUnionId);
        if (userOpt.isPresent()) {
            User user = userOpt.get();
            template.setOwnerId(user.getId());
            template.setCreatedAt(new Date());
            template.setUpdatedAt(new Date());
            return clockInTemplateRepository.save(template);
        }
        throw new MessageException("User not found");
    }

    public List<ClockInTemplateSummary> findAllByOwner(String wxUnionId) {
        Optional<User> userOpt = userRepository.findByWxUnionId(wxUnionId);
        if (userOpt.isEmpty()) {
            throw new MessageException("User not found");
        }
        User user = userOpt.get();
        return clockInTemplateRepository.findByOwnerId(user.getId());
    }

    public Optional<ClockInTemplate> findById(Long id, String wxUnionId) {
        Optional<ClockInTemplate> templateOpt = clockInTemplateRepository.findById(id);
        if (templateOpt.isEmpty()) {
            return Optional.empty();
        }
        Optional<User> userOpt = userRepository.findByWxUnionId(wxUnionId);
        if (userOpt.isEmpty()) {
            return Optional.empty();
        }
        if (templateOpt.get().getOwnerId().equals(userOpt.get().getId())) {
            return templateOpt;
        }
        return Optional.empty();
    }

    public Optional<ClockInTemplateSummary> findSummaryById(Long id, String wxUnionId) {
        Optional<User> userOpt = userRepository.findByWxUnionId(wxUnionId);
        if (userOpt.isEmpty()) {
            return Optional.empty();
        }
        return clockInTemplateRepository.findSummaryById(id, userOpt.get().getId());
    }

    public ClockInTemplate update(Long id, ClockInTemplate template, String wxUnionId) {
        Optional<ClockInTemplate> existingTemplateOpt = findById(id, wxUnionId);
        if (existingTemplateOpt.isPresent()) {
            ClockInTemplate existingTemplate = existingTemplateOpt.get();
            existingTemplate.updateBy(template);
            existingTemplate.setUpdatedAt(new Date());
            return clockInTemplateRepository.save(existingTemplate);
        }
        throw new MessageException("Template not found or unauthorized");
    }

    public void delete(Long id, String wxUnionId) {
        Optional<ClockInTemplate> templateOpt = findById(id, wxUnionId);
        if (templateOpt.isPresent()) {
            clockInTemplateRepository.deleteById(id);
        } else {
            throw new MessageException("Template not found or unauthorized");
        }
    }

}